package com.zj.osm.view.home.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.zj.osm.R;
import com.zj.osm.view.BaseFragment;
import com.zj.osm.view.home.home.home_fragment.onefragment.OneFragment;
import com.zj.osm.view.home.home.home_fragment.threefragment.ThreeFragment;
import com.zj.osm.view.home.home.home_fragment.twofragment.TwoFragment;

import java.util.List;

public class HomeBootomTabWidget extends LinearLayout implements View.OnClickListener {
    private FragmentManager mFragmentManager;
    private List<BaseFragment> mFragmentList;
    private TextView[] textViews;

    public HomeBootomTabWidget(Context context) {
        this(context, null);
    }


    public HomeBootomTabWidget(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HomeBootomTabWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = View.inflate(context, R.layout.home_bottom_tab, this);
        initView(view);
    }

    public HomeBootomTabWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);


    }

    /**
     * 外部调用初始化，传入必要的参数
     *
     * @param fm
     */
    public void init(FragmentManager fm) {
        mFragmentManager = fm;
        fragmentManger(new OneFragment());


    }

    /**
     * 初始化 设置点击事件。
     */
    private void initView(View view) {
        //默认第一个碎片
        textViews = new TextView[]{view.findViewById(R.id.bottom_btn_one), view.findViewById(R.id.bottom_btn_two), view.findViewById(R.id.bottom_btn_three)};
        for (TextView textView : textViews) {
            textView.setOnClickListener(this);
        }
        //设置默认的选择碎片。
        setColor(0);

    }

    /**
     * 实现按钮的点击事件
     */
    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bottom_btn_one:
                fragmentManger(new OneFragment());
                setColor(0);
                break;
            case R.id.bottom_btn_two:
                fragmentManger(new TwoFragment());
                setColor(1);
                break;
            case R.id.bottom_btn_three:
                fragmentManger(new ThreeFragment());
                setColor(2);
                break;
        }
    }


    /**
     * 实现底部导航栏的切换。
     *
     * @param i 传入按钮的序号
     */
    private void setColor(int i) {
        for (int j = 0; j < 3; j++) {
            if (i == j) {
                textViews[j].setTextColor(Color.rgb(18, 150, 219));
                textViews[j].setSelected(true);
            } else {
                textViews[j].setTextColor(Color.BLACK);
                textViews[j].setSelected(false);
            }
        }
    }


    /**
     * fragment的切换
     */
    private void fragmentManger(Fragment fragment) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.replace(R.id.content, fragment);
        transaction.commit();
    }


    public void goFragment() {
        fragmentManger(new TwoFragment());
        setColor(1);
    }
}
